<template>
    <div class="container-fluid" v-loading="loading">
        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header"><h4>文章编辑</h4></div>
                    <div class="card-body">
                        <course-form-component :form="form"></course-form-component>
                        <div class="row" style="margin-left: 20px;">
                            <div class="form-group" style="margin-left: 10px;">
                                <template v-if="form.verifyStatus">
                                    <p>
                                        <button type="submit" class="btn btn-primary"
                                                v-on:click="submit(form.verifyStatus,form.pushStatus)">保存
                                        </button>
                                    </p>
                                    <p>
                                        <button type="submit" class="btn btn-primary"
                                                v-on:click="submit(form.verifyStatus,1)">加推送
                                        </button>
                                    </p>
                                </template>
                                <template v-if="!form.verifyStatus">
                                    <p>
                                        <button type="submit" class="btn btn-primary"
                                                v-on:click="submit(-1,form.pushStatus)">不通过
                                        </button>
                                    </p>
                                    <p>
                                        <button type="submit" class="btn btn-primary"
                                                v-on:click="submit(1,form.pushStatus)">保存并通过审核
                                        </button>
                                    </p>
                                    <p>
                                        <button type="submit" class="btn btn-primary" v-on:click="submit(1,1)">
                                            通过审核并加推送
                                        </button>
                                    </p>
                                </template>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

</template>

<script>

    import common from '../../api/public.js';

    import CourseFormComponent from "./CourseFormComponent";

    export default {
        name: "CourseCreateComponent",
        components: {
            "course-form-component": CourseFormComponent,
        },

        data() {
            return {
                form:{
                    title: '',
                    describe:'',
                    imageUrl:'',
                    body:'',
                    status:1,
                    verifyStatus:1,
                    pushStatus:0
                },

                loading:false
            }
        },
        methods:{
            ...common,
            submit:function (pushStatus) {
                let self = this;
                let uri = "course/store";
                if (!self.form.title) {
                    self.$message.error("必须填写标题");
                    return;
                }
                if (!self.form.describe) {
                    self.$message.error("必须填写摘要");
                    return;
                }
                if (!self.form.imageUrl) {
                    self.$message.error("必须上传封面图");
                    return;
                }
                if (!self.form.body) {
                    self.$message.error("必须填写内容");
                    return;
                }
                self.form.pushStatus = pushStatus;
                self.loading = true;
                axios.post(uri,self.form).then(function (result) {
                    self.loading = false;
                    if (result.data) {
                        self.openUri('course/index');
                    } else {
                        self.$message.error("创建课程失败！");
                    }
                }).catch(function (error) {
                    self.loading = false;
                });
            },
        }
    }
</script>

<style scoped>

</style>
